﻿using AuthCenter.Data.Dto;
using AuthCenter.Data.Model._202411;
using Dapper;
using ExcelConfig;
using System;

namespace AuthCenter.Data.Database
{
    public class AuthLogDB : IDisposable
    {
        internal IRepo repo;
        public AuthLogDB()
        {
            var connectstring = AppConfig.Text("auth_log");
            repo = new MysqlRepo(connectstring);
        }

        public void Dispose()
        {
            repo.Dispose();
        }

        public void Record(string method, string msg, Guid appid, Guid? userid = null)
        {
            repo.Open();
            var log = new LogItem();
            log.LogID = Guid.NewGuid();
            log.AppID = appid;
            log.UserID = userid;
            log.Method = method;
            log.Text = msg;
            log.OccurTime = DateTime.UtcNow;

            using (var transaction = repo.Connection.BeginTransaction())
            {
                try
                {
                    repo.Connection.Execute("insert into ok_log (LogID, Method, Text, UserID, AppID, OccurTime) values (@LogID, @Method, @Text, @UserID, @AppID, @OccurTime);", log, transaction);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    // 发生异常时回滚事务
                    transaction.Rollback();
                    // 处理异常
                    Console.WriteLine(ex.Message);
                }
            }
        }
        public void RecordError(string method, string msg, Guid appid, Guid? userid = null)
        {
            repo.Open();
            var log = new LogItem();
            log.LogID = Guid.NewGuid();
            log.AppID = appid;
            log.UserID = userid;
            log.Method = method;
            log.Text = msg;
            log.OccurTime = DateTime.UtcNow;

            using (var transaction = repo.Connection.BeginTransaction())
            {
                try
                {
                    repo.Connection.Execute("insert into fail_log (LogID, Method, Text, UserID, AppID, OccurTime) values (@LogID, @Method, @Text, @UserID, @AppID, @OccurTime);", log, transaction);
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    // 发生异常时回滚事务
                    transaction.Rollback();
                    // 处理异常
                    Console.WriteLine(ex.Message);
                }
            }
        }
        public IList<LogInfo> GetLogs(Guid userId, int start, int count)
        {
            repo.Open();

            var infos = repo.Connection.Query<LogInfo>("select LogID, Method, Text, OccurTime from ok_log where UserID=@UserID;", new { UserID=userId }).OrderByDescending(log=> log.OccurTime).Skip(start).Take(count);
            return infos.ToList();
        }
        public decimal GetLogCount(Guid userId)
        {
            repo.Open();

            var length = repo.Connection.QueryFirstOrDefault<decimal>("select count(LogID) from ok_log;");
            return length;
        }

        public IList<LogInfo> GetFailLogs(Guid userId, int start, int count)
        {
            repo.Open();

            var infos = repo.Connection.Query<LogInfo>("select LogID, Method, Text, OccurTime from fail_log where UserID=@UserID;", new { UserID = userId }).OrderByDescending(log => log.OccurTime).Skip(start).Take(count);
            return infos.ToList();
        }
        public decimal GetFailLogCount(Guid userId)
        {
            repo.Open();

            var length = repo.Connection.QueryFirstOrDefault<decimal>("select count(LogID) from fail_log;");
            return length;
        }

    }
}
